<script setup>
import { reactive, ref } from "vue";
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";
import router from "@/router/index.js";

const data = reactive({
  formVisible: false,
  form: {
    username: '',
    password: '',
    role: 'ADMIN'
  },
  rules: {
    username: [
      { required: true, message: '请输入账号', trigger: 'blur' },
    ],
    password: [
      { required: true, message: '请输入名称', trigger: 'blur' },
    ],
  }
})

const formRef = ref()

//TODO
const login = () => {
  formRef.value.validate(valid => {
    if (valid){ //验证通过的情况下
      request.post('/login',data.form).then(res => {
        if (res.code === '200'){
          localStorage.setItem('code_user',JSON.stringify(res.data || {}))
          ElMessage.success("登录成功")
          router.push('/manager')
        }else {
          ElMessage.error(res.msg)
        }
      })
    }
  })
}

</script>

<template>
  <div class="bg">
    <div style="width: 350px;background-color: #fff;border-radius: 10px;box-shadow: 0 0 10px rgba(0,0,0,0.2);padding: 40px 20px">
      <el-form ref="formRef" :model="data.form" :rules="data.rules">
        <div style="margin-bottom: 40px;text-align: center;font-weight: bold;font-size: 20px">欢 迎 登 录</div>
        <el-form-item prop="username">
          <el-input size="large" v-model="data.form.username" autocomplete="off" prefix-icon="User" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input show-password v-model="data.form.password" autocomplete="off" prefix-icon="Lock" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item prop="role">
          <el-select size="large" style="width: 100%" v-model="data.form.role">
            <el-option label="管理员" value="ADMIN"></el-option>
          </el-select>
        </el-form-item>
        <div>
          <el-button size="large" type="primary" style="width: 100%;margin-bottom: 10px" @click="login">登 录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<style>
.bg{
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  overflow: hidden;
  background-image: url("@/assets/imgs/bg.png");
  background-size: cover;
}

</style>